package com.example.administrator.familfinalcialmanagement.ui.fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.FriendBean;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.TimeBean;
import com.example.administrator.familfinalcialmanagement.databinding.FragmentReasionBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.TimeBeanDao;
import com.example.administrator.familfinalcialmanagement.ui.adapter.FriendAdapter;
import com.example.administrator.familfinalcialmanagement.util.GlideUtil;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.example.administrator.familfinalcialmanagement.util.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.glide.transformations.BlurTransformation;

/**
 * Created by Administrator on 2019/3/30.
 */

public class ReasionFragment extends BaseLazyFragment {

    FragmentReasionBinding binding;
    List<TimeBean> list = new ArrayList<>();
    List<TimeBean> listSearche = new ArrayList<>();
    FriendAdapter adapter;

    TimeBeanDao timeBeanDao = GreenDaoUtils.getSingleTon().getmDaoSession().getTimeBeanDao();

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_reasion, container, false);
        binding.setFragment(this);
        return binding.getRoot();
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void lazyLoad() {
        list.clear();
        list = timeBeanDao.queryBuilder().orderDesc(TimeBeanDao.Properties.Five).list();
        listSearche.clear();
        if (timeBeanDao.loadAll().size() == 0) {
            binding.recycler.setVisibility(View.GONE);
            binding.ilModuleNothing.setVisibility(View.VISIBLE);
        } else {
            binding.recycler.setVisibility(View.VISIBLE);
            binding.ilModuleNothing.setVisibility(View.GONE);
            listSearche.addAll(list);

            adapter = new FriendAdapter(getActivity(), listSearche);
            binding.recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
            binding.recycler.setAdapter(adapter);

            binding.etModuleSearch.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    LogUtil.e("输入-->" + s);
                    listSearche.clear();
                    for (TimeBean timeBean : list) {
                        LogUtil.e("n-->"+ timeBean.getName());
                        if (timeBean.getName().contains(s.toString())) {
                            LogUtil.e("添加数据-->" + timeBean.getName() + " : " + s);
                            listSearche.add(timeBean);
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public int setLayoutId() {
        return R.layout.fragment_index;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            default:
        }
    }
}